package com.codegeek.project.slope.controller;

import com.alibaba.fastjson.JSON;
import com.codegeek.framework.TaskTimer;
import com.codegeek.framework.aspectj.lang.annotation.Log;
import com.codegeek.framework.aspectj.lang.enums.BusinessType;
import com.codegeek.framework.web.controller.BaseController;
import com.codegeek.framework.web.domain.AjaxResult;
import com.codegeek.project.communicate.SlopeDTUService;
import com.codegeek.project.slope.domain.SlopeAlarm;
import com.codegeek.project.slope.domain.SlopeGlare;
import com.codegeek.project.slope.service.ISlopeGlareService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wj
 * @ClassName
 * @Description
 * @date 2020-03-27
 */
@RestController
@RequestMapping("/slope/index")
@Api(value="首页Controller",tags={"首页操作接口"})
public class SlopeIndexController extends BaseController {

    @Autowired
    private ISlopeGlareService slopeGlareService;

    @Autowired
    private SlopeDTUService slopeDTUService;

    /**
     * 关闭所有声光报警器
     */
    @ApiOperation("关闭或开启所有声光报警器")
    @Log(title = "首页", businessType = BusinessType.OPERATION)
    @PreAuthorize("@ss.hasPermi('slope:index:glare')")
    @PostMapping("/controlGlare")
    public AjaxResult controlGlare(@RequestBody String on)
    {
        List<SlopeGlare> slopeGlareList = slopeGlareService.selectSlopeGlareList(new SlopeGlare());
        boolean isOnline=false;
        for(SlopeGlare slopeGlare:slopeGlareList){{
            if(slopeGlare.getDevice().getState()==1) {
                isOnline = true;
            }
        }}

        if(isOnline){
            for(SlopeGlare slopeGlare:slopeGlareList){{
                slopeDTUService.controlAlarmDevice(slopeGlare.getDevice().getImei(),Boolean.parseBoolean(on));
            }}
            return AjaxResult.success("操作成功");
        }else {
            return AjaxResult.error("操作失败，报警器离线");
        }
    }

    @ApiOperation("获取声光报警器状态")
    @PreAuthorize("@ss.hasPermi('slope:index:glare')")
    @GetMapping("/getGlare")
    public AjaxResult getGlare()
    {
        List<SlopeGlare> slopeGlareList = slopeGlareService.selectSlopeGlareList(new SlopeGlare());
        boolean on=false;
        for(SlopeGlare slopeGlare:slopeGlareList){
            String reserve= slopeGlare.getReserve();
            if(slopeGlare.getDevice().getState()==1&&reserve!=null&&reserve.equals("1")){
                on=true;
            }
        }
        return AjaxResult.success(on);
    }

    @ApiOperation("获取声光报警器启用状态")
    @PreAuthorize("@ss.hasPermi('slope:index:glare')")
    @GetMapping("/isGlareOnline")
    public AjaxResult isGlareOnline()
    {
        boolean isOnline=false;
        List<SlopeGlare> slopeGlareList = slopeGlareService.selectSlopeGlareList(new SlopeGlare());
        for(SlopeGlare slopeGlare:slopeGlareList){
            if(slopeGlare.getDevice().getState()==1){
                isOnline=true;
            }
        }
        return AjaxResult.success(isOnline);
    }


    @Autowired
    private TaskTimer taskTimer;

    private String weatherUrl = "https://tianqiapi.com/api?version=v6&appid=12365866&appsecret=3osBAiLP&vue=1";
    @ApiOperation("获取天气")
    @GetMapping("/getWeather")
    public AjaxResult getWeather()
    {
        return AjaxResult.success("操作成功！",taskTimer.getWeatherMap());
    }

}
